<!--
 * @Description: 首保提醒
 * @Autor: 刘诗文
 * @Date: 2020-12-04 09:43:07
 * @LastEditors: 马毅
 * @LastEditTime: 2021-07-13 13:41:49
-->
<template>
  <div>
    <!-- 搜索框 -->
    <TopFilter :list="topFilterList" :cols="3" :labelWidth="'120'" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <ButtonArea :style="{ paddingLeft: '0px' }">
      <el-button type="primary" @click="showDia(1)">提醒</el-button>
      <el-button type="primary" @click="showDia(2)">提醒历史</el-button>
      <el-button type="primary" @click="showDia(3)">预约安排</el-button>
      <el-button type="primary" @click="showDia(4)">维修历史</el-button>
      <el-button type="primary" @click="showDia(5)">维修建议</el-button>
      <el-button type="primary" @click="showDia(6)">投诉历史</el-button>
      <el-button type="primary" :loading="exportLoading" @click="exportData()">导出</el-button>
    </ButtonArea>

    <!-- 新车提醒表格 -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :selectionType="'single'"
      columnsRef="khghXctxTable"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="onRowSelectChange"
    >
    </FilterTable>

    <!-- 提醒录入 -->
    <BaseDialog :visible.sync="showRemind" title="提醒录入" width="1300" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <RemindInputs ref="RemindInputs" :formData="selectData" :remindSource="81991001" @close="cancle(1)" />
    </BaseDialog>

    <!-- 提醒历史 -->
    <BaseDialog :visible.sync="showRemindRecord" title="提醒历史" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <remindHistory :remindId="remindId" :remindSource="5"></remindHistory>
      <div
        :style="{
          position: 'absolute',
          left: 0,
          bottom: 0,
          right: 0,
          zIndex: 9999,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }"
      >
        <el-button @click="cancle(2)">返 回</el-button>
      </div>
    </BaseDialog>

    <!-- 预约安排 -->
    <Drawer :visible.sync="showReservation" title="预约安排" width="1350" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <precontractArrange :defaultData="this.resData" defaultPageType="create" @close="cancle(3)" />
    </Drawer>

    <!-- 维修历史 -->
    <BaseDialog :visible.sync="showRepairHistory" title="维修历史" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <repairHistory :carInfo="carInfoObj" :vin="carVin" @close="cancle(4)"></repairHistory>
    </BaseDialog>

    <!-- 维修建议 -->
    <BaseDialog :visible.sync="showRepairSuggest" title="维修建议" width="70%" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <maintenanceAdvice :vin="carVin" @close="cancle(5)" />
    </BaseDialog>

    <!-- 投诉历史 -->
    <Drawer :visible.sync="showComplaintHistory" title="投诉历史" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <complaintHistory ref="complaintHistory" :vin="chooseVin" @close="cancle(6)"></complaintHistory>
    </Drawer>
  </div>
</template>
<script>
import _ from 'lodash';
import moment from 'moment';
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { queryVehicleRemind, exportVehicleRemind } from '@/api/customerRepair/customerCare/vehicleRemind';
import { getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import { exportExcelFile } from '@/utils/index';
import { getUserListSelect } from '@/api/customerRepair/customerCare/maintenanceRemind/index';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import dictCode from '@/utils/dictCode';
import { jsonToHump } from '@/utils/index';
import RemindInputs from './reminderEntry.vue';
import RemindHistory from '@/pages/customerRepair/customerCare/vehicleRemind/components/remindHistory.vue';
import repairHistory from '@/pages/repair/repairReception/customer/repairHistory'; // 维修历史
import complaintHistory from '@/pages/repair/business/customer/combination/complaint';
import maintenanceAdvice from '@/pages/customerRepair/afterSaleVisit/resultEntry/maintenanceAdvice.vue'; // 维修建议
import precontractArrange from '@/pages/repair/repairReception/precontractArrange'; // 预约安排
import { getSeries, getModel, getRegion } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
import { getPinpSelect, getChexSelect, getCheModelSelect } from '@/api/repair/repairReception/customer/brand.js';
import { notifyAction } from '../../../../utils';

export default {
  name: 'VehicleRemind',
  components: {
    RemindInputs,
    RemindHistory,
    repairHistory,
    complaintHistory,
    maintenanceAdvice,
    precontractArrange
  },
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    this.resData = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: queryVehicleRemind, // 获取新车提醒表格接口
      fetchParams: { reminderType: 81991001 }, // 获取新车提醒接口参数
      columns: this.createTableColumns(), // 新车提醒表格列
      choose: [], // 选中的对象
      showRemind: false, // 提醒录入
      showRemindRecord: false, // 提醒历史
      showReservation: false, // 预约安排
      showRepairHistory: false, // 维修历史
      showRepairSuggest: false, // 建议维修
      showComplaintHistory: false, // 投诉历史
      exportLoading: false, // 导出加载
      chooseVin: null, // 选中的vin
      remindId: null,
      chooseLicense: null, // 选中的license
      chooseOwnerNo: null, // 选中的数据的ownerNo
      selectData: null, // 选中的数据（供提醒录入用）
      repairHistoryData: {}, // 维修历史数据
      carVin: '', // 维修历史需要底盘号参数
      carInfoObj: {} // 维修历史需要的对象
    };
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.serviceConsultant(); // 获取服务顾问
    this.getEmp();
    this.queryRegion();
    this.getBrand();
  },
  methods: {
    // 首保提醒搜索列
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brand',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '车主',
          fieldName: 'ownerName'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin'
        },
        {
          type: 'RANGE_DATE',
          label: '最近提醒日期',
          hidden: true,
          fieldName: 'remindDateFrom|remindDateTo',
          dateType: 'exactdaterange',
          change: val => {
            this.selectChange(val);
          }
        },
        {
          type: 'RANGE_DATE',
          label: '上次维修日期',
          hidden: true,
          fieldName: 'lastMaintainFromDate|lastMaintainToDate',
          dateType: 'exactdaterange'
        },
        {
          type: 'RANGE_DATE',
          label: '首次进厂日期',
          hidden: true,
          fieldName: 'firstInDateFrom|firstInDateTo',
          dateType: 'exactdaterange'
        },

        {
          type: 'RANGE_DATE',
          label: '销售日期',
          fieldName: 'salesDateFrom|salesDateTo',
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT',
          label: '车辆里程数',
          hidden: true,
          fieldName: 'milleage'
        },
        {
          type: 'SELECT',
          label: '提醒状态',
          fieldName: 'remindStatus',
          itemList: this.createDictList('8057')
        },
        {
          type: 'SELECT',
          label: '是否提醒回厂',
          fieldName: 'isRemindFactory',
          itemList: this.createDictList('1004')
        },
        // {
        //   type: 'SELECT',
        //   label: '是否回厂',
        //   fieldName: 'isValid',
        //   itemList: this.createDictList('1004')
        // },
        {
          type: 'SELECT',
          label: '上次接待服务顾问',
          hidden: true,
          fieldName: 'lastConsultantId',
          filterable: true,
          itemList: []
        }
      ];
    },
    // 首保提醒表格列
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '车牌号',
          dataIndex: 'license'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 150
        },
        {
          title: '行驶里程',
          dataIndex: 'mileage'
        },
        // {
        //   title: '是否回厂',
        //   dataIndex: 'isRemindFactory',
        //   sorter: true,
        //   render: props => {
        //     return <span>{this.createDictText(props.row.IS_REMIND_FACTORY, '1004')}</span>;
        //   }
        // },
        {
          title: '品牌',
          dataIndex: 'brandName'
        },
        {
          title: '车系',
          dataIndex: 'seriesName'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          width: 100
        },
        {
          title: '车主编号',
          dataIndex: 'ownerNo'
        },
        {
          title: '车主',
          dataIndex: 'ownerName',
          showOverflowTooltip: true
        },
        {
          title: '车主电话',
          dataIndex: 'phone',
          showOverflowTooltip: true
        },
        {
          title: '车主手机',
          dataIndex: 'mobile',
          showOverflowTooltip: true
        },
        {
          title: '车主地址',
          dataIndex: 'address',
          showOverflowTooltip: true
        },
        {
          title: '车主性质',
          dataIndex: 'ownerProperty',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.ownerProperty, '8011')}</span>;
          }
        },
        // {
        //   title: '送修人',
        //   dataIndex: 'deliverer',
        //   sorter: true
        // },
        // {
        //   title: '送修人手机',
        //   dataIndex: 'delivererMobile',
        //   showOverflowTooltip: true,
        //   sorter: true
        // },
        // {
        //   title: '送修人电话',
        //   dataIndex: 'delivererPhone',
        //   showOverflowTooltip: true,
        //   sorter: true
        // },
        // {
        //   title: '联系人',
        //   dataIndex: 'CONTACTOR_NAME',
        //   showOverflowTooltip: true,
        //   sorter: true
        // },
        // {
        //   title: '联系人手机',
        //   dataIndex: 'CONTACTOR_MOBILE',
        //   showOverflowTooltip: true,
        //   sorter: true
        // },
        // {
        //   title: '联系人电话',
        //   dataIndex: 'CONTACTOR_PHONE',
        //   showOverflowTooltip: true,
        //   sorter: true
        // },
        // {
        //   title: '联系人邮编',
        //   dataIndex: 'CONTACTOR_ZIP_CODE',
        //   showOverflowTooltip: true,
        //   sorter: true
        // },
        // {
        //   title: '联系人详细地址',
        //   dataIndex: 'CONTACTOR_ADDRESS',
        //   showOverflowTooltip: true,
        //   sorter: true
        // },
        // {
        //   title: '上次维修日期',
        //   dataIndex: 'lastMaintainDate',
        //   sorter: true,
        //   dateFormat: 'yyyy-MM-dd'
        // },
        // {
        //   title: '上次维修里程',
        //   dataIndex: 'lastMaintainMileage',
        //   showOverflowTooltip: true,
        //   sorter: true
        // },
        // {
        //   title: '预计下次保养日期',
        //   dataIndex: 'nextMaintainDate',
        //   sorter: true,
        //   dateFormat: 'yyyy-MM-dd'
        // },
        {
          title: '下次保养里程',
          dataIndex: 'nextMaintainMileage',
          showOverflowTooltip: true,
          sorter: true
        },
        {
          title: '服务顾问',
          dataIndex: 'serviceAdvisor'
        },
        // {
        //   title: '上次接待服务顾问',
        //   dataIndex: 'lastConsultantId'
        // },
        {
          title: '提醒人',
          dataIndex: 'remindName'
        },
        {
          title: '提醒内容',
          dataIndex: 'remindRemark',
          showOverflowTooltip: true
        },
        {
          title: '提醒状态',
          dataIndex: 'remindStatus',
          render: props => {
            return <span>{this.createDictText(props.row.remindStatus, '8057')}</span>;
          }
        },
        {
          title: '提醒方式',
          dataIndex: 'remindMode',
          render: props => {
            return <span>{this.createDictText(props.row.remindMode, '8081')}</span>;
          }
        },
        {
          title: '提醒失败原因',
          dataIndex: 'remindFail',
          render: props => {
            return <span>{this.createDictText(props.row.remindFail, '8069')}</span>;
          }
        },
        {
          title: '客户反馈',
          dataIndex: 'customerFeedback',
          showOverflowTooltip: true
        },
        {
          title: '销售日期',
          dataIndex: 'salesDate',
          dateFormat: 'yyyy-MM-dd'
        }
        // {
        //   title: '定保专员',
        //   dataIndex: 'maintainAdvisor'
        // },
        // {
        //   title: '续保专员',
        //   dataIndex: 'insuranceAdvisor'
        // },
        // {
        //   title: '第一次进厂日期',
        //   dataIndex: 'firstInDate',
        //   sorter: true,
        //   dateFormat: 'yyyy-MM-dd'
        // }
      ];
    },
    // 新车提醒搜索
    changeHandle(val) {
      val.reminderType = 81991001;
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    onRowSelectChange(row) {
      if (!row.length) {
        this.selectData = null;
      } else {
        this.selectData = row[0];
      }
    },
    // 显示弹框
    showDia(type) {
      if (this.selectData === null) {
        this.$message.warning('请选择一条数据');
        return;
      }
      this.chooseVin = this.selectData.VIN;
      this.chooseLicense = this.selectData.LICENSE;
      this.chooseOwnerNo = this.selectData.OWNER_NO;
      switch (type) {
        case 1:
          // 打开提醒
          this.selectData = _.cloneDeep(this.selectData);
          // jsonToHump(this.selectData);
          this.showRemind = true;
          break;
        case 2:
          // 打开提醒记录
          this.showRemindRecord = true;
          this.remindId = this.selectData.remindId;
          break;
        case 3:
          // 打开预约安排
          this.showReservation = true;
          this.resData = this.selectData;
          break;
        case 4:
          // 打开维修历史
          this.carVin = this.selectData.vin;
          this.carInfoObj = this.selectData;
          this.showRepairHistory = true;
          break;
        case 5:
          // 打开建议维修
          this.carVin = this.selectData.vin;
          this.showRepairSuggest = true;
          break;
        case 6:
          // 打开投诉历史
          this.showComplaintHistory = true;
          break;
      }
    },
    // 取消,关闭弹框
    cancle(type) {
      switch (type) {
        case 1:
          // 关闭提醒
          this.showRemind = false;
          break;
        case 2:
          // 关闭提醒记录
          this.showRemindRecord = false;
          break;
        case 3:
          // 关闭预约安排
          this.showReservation = false;
          break;
        case 4:
          // 关闭维修历史
          this.showRepairHistory = false;
          break;
        case 5:
          // 关闭建议维修
          this.showRepairSuggest = false;
          break;
        case 6:
          // 关闭投诉历史
          this.showComplaintHistory = false;
          break;
      }
    },
    async serviceConsultant() {
      // let roleParam = { role: dictCode.SERVICE_CONSULTANT };
      // let res = await searchRoleByCode(roleParam);
      // // 上次接待服务顾问
      // // const resId = await getUserListSelect()
      // this.topFilterList.find(item => item.fieldName === 'lastConsultantId').itemList = res.data.map(x => ({ text: x.employeeName, value: x.userId }));
      // this.columns.find(item => item.dataIndex === 'LAST_CONSULTANT_ID').editItems = res.data.map(x => ({ text: x.employeeName, value: x.userId }));
    },
    // 获取全部员工
    async getEmp() {
      // const res = await getDealerUser(this.editComplaintId);
      // let empList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      // this.columns.find(item => item.dataIndex === 'MAINTAIN_ADVISOR').editItems = empList; // 定保专员
      // this.columns.find(item => item.dataIndex === 'INSURANCE_ADVISOR').editItems = empList; // 续保专员
      // this.columns.find(item => item.dataIndex === 'SERVICE_ADVISOR').editItems = empList; // 服务顾问
      this.columns.find(item => item.dataIndex === 'REMINDER').editItems = this.columns.find(item => item.dataIndex === 'REMINDER').dictItems = empList; // 提醒人
    },
    // 查询省市(供table使用)
    async queryRegion() {
      const res = await getRegion();
      // let list = res.data.map(x => ({ text: x.REGION_NAME, value: x.REGION_ID }));
      // this.columns.find(x => x.dataIndex === 'PROVINCE').editItems = list;
      // this.columns.find(x => x.dataIndex === 'CITY').editItems = list;
    },
    // 品牌
    async getBrand() {
      const res = await getPinpSelect();
      this.topFilterList.find(item => item.fieldName === 'brand').itemList = res.data.map(x => ({
        value: x.brandId + '',
        text: x.brandName
      }));
    },
    // 导出
    exportData() {
      this.exportLoading = true;
      exportVehicleRemind(this.fetchParams).then(res => {
        exportExcelFile(res.data, '首保提醒');
      });
      this.exportLoading = false;
    }
    // selectChange(val) {
    //   this.topFilterList.find(item => item.fieldName === 'isRemindFactory').disabled = !val[0] && !val[1];
    //   this.topFilterList.find(item => item.fieldName === 'isRemindFactory').initialValue = null;
    // }
  }
};
</script>
